package com.hgkj.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hgkj.common.core.domain.ApiResult;
import com.hgkj.common.core.domain.BaseController;
import com.hgkj.common.core.utils.AssertExceptionUtils;
import com.hgkj.system.domain.SysUser;
import com.hgkj.system.domain.dto.request.AssignUserRole;
import com.hgkj.system.domain.dto.request.UserPasswordRequest;
import com.hgkj.system.domain.dto.request.UserStatusRequest;
import com.hgkj.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.List;

@Validated
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService userService;

    /**
     * 分页查询用户信息
     *
     * @param user 查询条件
     * @return 分页数据
     */
    @GetMapping
    @SaCheckPermission("system.user.list")
    public ApiResult list(SysUser user) {
        startPage();
        List<SysUser> users = userService.getUserList(user);
        return getTableData(users);
    }

    /**
     * 添加用户数据
     *
     * @param user user
     * @return resp
     */
    @PostMapping
    @SaCheckPermission("system.user.add")
    public ApiResult add(@Valid @RequestBody SysUser user) {
        return getResult(userService.insertUser(user));
    }

    /**
     * 修改用户信息
     *
     * @param user userInfo
     * @return Resp
     */
    @PutMapping
    @SaCheckPermission("system.user.edit")
    public ApiResult update(@RequestBody SysUser user) {
        AssertExceptionUtils.notNull(user.getUserId(), "userId字段不能为空");
        return getResult(userService.updateUser(user));
    }

    /**
     * 删除用户信息
     *
     * @param userId userId
     * @return Response
     */
    @DeleteMapping("/{userId}")
    @SaCheckPermission("system.user.remove")
    public ApiResult delete(@PathVariable @NotNull(message = "用户ID不能为空")
                            @Min(value = 2, message = "用户ID必须大于1") Long userId) {
        return getResult(userService.deleteUserByUserId(userId));
    }

    /**
     * 修改用户密码
     *
     * @param passwordRequest password
     * @return RESP
     */
    @PutMapping("/updatePassword")
    @SaCheckPermission("system.user.updatePassword")
    public ApiResult updatePassword(@Valid @RequestBody UserPasswordRequest passwordRequest) {
        return getResult(userService.updatePassword(passwordRequest));
    }

    /**
     * 修改用户状态
     *
     * @param request 用户状态变更
     * @return Resp
     */
    @PutMapping("/freeze")
    @SaCheckPermission("system.user.freeze")
    public ApiResult freeze(@Valid @RequestBody UserStatusRequest request) {
        return getResult(userService.freezeUserById(request));
    }

    /**
     * 查询用户已分配角色
     * @param userId userId
     * @return Resp
     */
    @SaCheckPermission("system.user.assign")
    @GetMapping("/assign/{userId}")
    public ApiResult assignList(@PathVariable @NotNull(message = "用户ID不能为空")
                                @Min(value = 2, message = "用户ID必须大于1") Long userId) {
        return success(userService.assignList(userId));
    }

    /**
     * 为用户分配角色
     * @param assignUserRole data
     * @return resp
     */
    @SaCheckPermission("system.user.assign")
    @PutMapping("/assign")
    public ApiResult assign(@Valid @RequestBody AssignUserRole assignUserRole){
        userService.assignUserRole(assignUserRole);
        return success();
    }
}
